package com.hourse.service;

import com.hourse.dao.HouseDao;
import com.hourse.pojo.HouseSearch;
import com.hourse.util.IdWorker;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

@Service
public class HouseSeachService {
    @Autowired
    private HouseDao houseDao;
    @Autowired
    private IdWorker idWorker;

    public void save(HouseSearch search){
        search.setId(idWorker.nextId());
        houseDao.save(search);
    }

    public Page<HouseSearch> findBykey(String key, Integer stratprice, Integer kaipan, Integer wuye, Integer status
                                       ,Integer tese, Integer type, Integer page, Integer size,Integer endprice,Integer isdesc) {
        Pageable of = PageRequest.of(page - 1, size);
        // 构建器
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

                    queryBuilder.withPageable(PageRequest.of(page-1,size))
                            .withHighlightFields(new HighlightBuilder.Field(key))
                            .build();

        if (key!=null&&!"".equals(key)){
            queryBuilder.withQuery(QueryBuilders.queryStringQuery(key).defaultField("house_title") );
        }
        if (stratprice!=null){
            queryBuilder.withQuery(QueryBuilders.rangeQuery("house_price").gte(stratprice));
        }
        if (endprice!=null){
            queryBuilder.withQuery(QueryBuilders.rangeQuery("house_price").lte(endprice));
        }
        if (kaipan!=null){
            queryBuilder.withQuery(QueryBuilders.matchQuery("house_kaipan",kaipan));
        }
        if (wuye!=null){
            queryBuilder.withQuery(QueryBuilders.matchQuery("house_wuye",wuye));
        }
        if (status!=null){
            queryBuilder.withQuery(QueryBuilders.matchQuery("house_status",status));
        }
        if (tese!=null){
            queryBuilder.withQuery(QueryBuilders.matchQuery("house_tese",tese));
        }
        if (type!=null){
            queryBuilder.withQuery(QueryBuilders.matchQuery("house_type",type));
        }
        if (isdesc!=null&&isdesc==1){
            //按照price字段升序
            queryBuilder.withSort(SortBuilders.fieldSort("house_price").order(SortOrder.ASC));
        }else if (isdesc!=null&&isdesc==2){
            //按照price字段降序
            queryBuilder.withSort(SortBuilders.fieldSort("house_price").order(SortOrder.DESC));
        }

        return houseDao.search(queryBuilder.build());
    }

    public void delkey(long id) {
        houseDao.deleteById(id);
    }
}
